package com.along.currentlimiting.redis.controller;

import com.along.currentlimiting.redis.service.RedisRateLimiterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/*
 * @Author: liujinyi
 * @DateTime: 2025/2/17 17:52
 */
@RestController
public class RedisRateLimiterController {

    @Autowired
    private RedisRateLimiterService redisRateLimiterService;

    @GetMapping("/Redislimited")
    public String limitedEndpoint() {
        String key = "rate_limit_key";
        int limit = 10; // 每秒允许10个请求
        int timeout = 1; // 1秒

        if (redisRateLimiterService.tryAcquire(key, limit, timeout)) {
            return "Request allowed";
        } else {
            return "Request blocked due to rate limiting";
        }
    }
}
